package com.mall.db.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.OmsCompanyAddressMapper;
import com.mall.db.mapper.OmsOrderReturnApplyMapper;
import com.mall.domain.OmsCompanyAddress;
import com.mall.domain.OmsOrderReturnApply;
import com.mall.dto.OrderReturnDto;
import com.mall.dto.ReturnApplyDto;
import com.mall.interface1.api.OmsOrderReturnApplyApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class OmsOrderReturnApplyApiImpl implements OmsOrderReturnApplyApi {

    @Autowired
    private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;

    @Autowired
    private OmsCompanyAddressMapper omsCompanyAddressMapper;


    //批量删除退货申请
    public void deleteReturn(List<Long> ids) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderReturnApply::getId,ids);
        omsOrderReturnApplyMapper.delete(queryWrapper);
    }

    //分页查询退货申请
    public List<OmsOrderReturnApply> list(ReturnApplyDto dto) {
        Page<OmsOrderReturnApply> pageInfo = new Page<>(dto.getPageNum(),dto.getPageSize());
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getCreateTime() != null,OmsOrderReturnApply::getCreateTime,dto.getCreateTime())
                .eq(StrUtil.isNotEmpty(dto.getHandleMan()),OmsOrderReturnApply::getMemberUsername,dto.getHandleMan())
                .eq(dto.getId() != null,OmsOrderReturnApply::getOrderId,dto.getId())
                .eq(dto.getStatus() != null,OmsOrderReturnApply::getStatus,dto.getStatus());
        queryWrapper.eq(StrUtil.isNotEmpty(dto.getReceiveKeyword()),OmsOrderReturnApply::getReturnName,dto.getReceiveKeyword())
                .or()
                .eq(StrUtil.isNotEmpty(dto.getReceiveKeyword()),OmsOrderReturnApply::getReturnPhone,dto.getReceiveKeyword());
        Page<OmsOrderReturnApply> omsOrderReturnApplyPage = omsOrderReturnApplyMapper.selectPage(pageInfo, queryWrapper);
        return omsOrderReturnApplyPage.getRecords();
    }

    @Override
    public Integer count(ReturnApplyDto dto) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getCreateTime() != null,OmsOrderReturnApply::getCreateTime,dto.getCreateTime())
                .eq(StrUtil.isNotEmpty(dto.getHandleMan()),OmsOrderReturnApply::getMemberUsername,dto.getHandleMan())
                .eq(dto.getId() != null,OmsOrderReturnApply::getOrderId,dto.getId())
                .eq(dto.getStatus() != null,OmsOrderReturnApply::getStatus,dto.getStatus());
        queryWrapper.eq(StrUtil.isNotEmpty(dto.getReceiveKeyword()),OmsOrderReturnApply::getReturnName,dto.getReceiveKeyword())
                .or()
                .eq(StrUtil.isNotEmpty(dto.getReceiveKeyword()),OmsOrderReturnApply::getReturnPhone,dto.getReceiveKeyword());
        return omsOrderReturnApplyMapper.selectCount(queryWrapper);
    }

    //修改退货申请状态
    public void updateStatus(Long id, OmsOrderReturnApply omsOrderReturnApply) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,OmsOrderReturnApply::getId,id);
        omsOrderReturnApplyMapper.updateById(omsOrderReturnApply);
    }

    //获取退货申请详情
    public OmsOrderReturnApply findReturnById(Long id) {
        return omsOrderReturnApplyMapper.selectById(id);
    }

    //根据地址id获取地址
    public OmsCompanyAddress findAddressById(Long companyAddressId) {
        return omsCompanyAddressMapper.selectById(companyAddressId);
    }


}
